package com.lz.repository.xjl.impl;

import com.lz.entity.*;
import com.lz.repository.xjl.EquipmentSheetDao;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.*;


/**
 * @author 徐建林
 * @date 2018/10/21 20:54
 */
@Repository
public class EquipmentSheetDaoImpl extends HibernateDaoSupport implements EquipmentSheetDao {
    /**
     * HibernateDaoSupport中导入了spring配置的sessionFactory
     */

    @Resource(name = "sessionFactory")
    public void setSessionFacotry(SessionFactory sessionFacotry) {
        super.setSessionFactory(sessionFacotry);

    }


    @Override
    public List<EquipmentSheet> getEquipmentSheetList(Integer pageNo, Integer pageSize, Map<String, Object> map) {
        StringBuffer hql = new StringBuffer("select e from  EquipmentSheet e,Car c,Customer cu where c.carId=e.car.carId and cu.customerId=e.customer.customerId");
       /* StringBuffer hql = new StringBuffer("select " +
                "equipmentSheet.equipmentSheetNo,customer.customerId,customer.customerName," +
                "customer.customerTel,car.vin,equipmentSheet.openingTime,equipmentSheet.totalPrice," +
                "equipmentSheet.givenMoney,equipmentSheet.receivables,equipmentSheet.received,equipmentSheet.equipmentSheetState"+
                " from  equipmentSheet,car,customer  where 1=1 and car.carId=equipmentSheet.carId and customer.customerId=equipmentSheet.customerId");
*/

        if (map!=null){
            if (map.get("equipmentSheetNo") != null && !"".equals(map.get("equipmentSheetNo"))) {
                hql.append(" and e.equipmentSheetNo like '%" + map.get("equipmentSheetNo") + "%'");
            }
            if (map.get("customerName") != null && !"".equals(map.get("customerName"))) {
                hql.append(" and e.customer.customerName like '%" + map.get("customerName") + "%'");
            }
            if (map.get("vin") != null && !"".equals(map.get("vin"))) {
                hql.append(" and e.car.vin like '%" + map.get("vin") + "%'");
            }
            if (map.get("carCard") != null && !"".equals(map.get("carCard"))) {
                hql.append(" and e.car.carCard like '%" + map.get("carCard") + "%'");
            }

            if (map.get("upholsteryType") != null && !"-1".equals(map.get("upholsteryType"))) {
                hql.append(" and e.upholsteryType.upholsteryType ='" + map.get("upholsteryType")+"'");
            }
            if (map.get("openingTime1") != null && !"".equals(map.get("openingTime1"))) {
                hql.append(" and e.openingTime >='" + map.get("openingTime1")+"'");
            }
            if (map.get("openingTime2") != null && !"".equals(map.get("openingTime2"))) {
                hql.append(" and e.openingTime <='" + map.get("openingTime2")+"'");
            }
            if (map.get("equipmentSheetState") != null && !"-1".equals(map.get("equipmentSheetState"))) {
                hql.append(" and e.equipmentSheetState ='" + map.get("equipmentSheetState")+"'");
            }

        }

        Session session = getSessionFactory().openSession();


        Query query = session.createQuery(hql.toString());
        query.setFirstResult((pageNo - 1) * pageSize);
        query.setMaxResults(pageSize);
        List list = query.list();
        System.out.println();
        session.close();



        return list;
    }

    @Override
    public List<EquipmentSheet> getEquipmentSheetList(Integer pageNo, Integer pageSize) {
        Session session = getSessionFactory().getCurrentSession();
        List list = session.createQuery("from EquipmentSheet").setFirstResult((pageNo - 1) * pageSize).setMaxResults(pageSize).list();
        return list;
    }

    @Override
    public EquipmentSheet getEquipmentById(Integer equipmentSheetNo) {
        System.out.println(equipmentSheetNo);
        return getHibernateTemplate().get(EquipmentSheet.class, equipmentSheetNo);
    }



    @Override
    public Integer getMaxPrimaryKey() {
        Session session = getSessionFactory().openSession();
        Object o = session.createQuery("select max(equipmentSheetNo) from EquipmentSheet").uniqueResult();

        session.close();
        if (o==null){
            return 0;
        }
        return (Integer) o;
    }

    @Override
    public Integer removeEquipmentSheetByEquipmentSheetNo(Integer equipmentSheetNo) {
        EquipmentSheet equipmentSheet = getHibernateTemplate().get(EquipmentSheet.class, equipmentSheetNo);
        getHibernateTemplate().delete(equipmentSheet);
        return equipmentSheetNo;
    }

    @Override
    public Integer saveEquipmentSheet(EquipmentSheet equipmentSheet, Car car, Customer customer, List<TemporaryCarParts> temporaryCarPartsList, Employee employee, UpholsteryType upholsteryType) {
        Session session = getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        session.save(customer);
        UpholsteryType type = (UpholsteryType) session.createQuery("from UpholsteryType where upholsteryType=:upholsteryType").setParameter("upholsteryType", upholsteryType.getUpholsteryType()).uniqueResult();
        equipmentSheet.setUpholsteryType(type);
        car.setCustomer(customer);
        session.save(car);
        equipmentSheet.setCar(car);
        //存储集合
        equipmentSheet.setOpeningTime(new Date());
        equipmentSheet.setReceived(equipmentSheet.getReceivables());
        equipmentSheet.setEquipmentSheetState("正在审核");





        //将集合存入equipmentSheet

        Employee o = (Employee)session.get(Employee.class, employee.getEmployeeId());

        equipmentSheet.setEmployee(o);
        equipmentSheet.setCustomer(customer);
        session.save(equipmentSheet);


        for (TemporaryCarParts t:temporaryCarPartsList
                ) {
            t.setEquipmentSheetNo(equipmentSheet.getEquipmentSheetNo());

            session.save(t);

        }

        transaction.commit();
        session.close();



        return equipmentSheet.getEquipmentSheetNo();
    }

    @Override
    public void deleteByEquipmentSheetNO(Integer equipmentSheetNO) {
        Session session = getSessionFactory().openSession();
        EquipmentSheet e = (EquipmentSheet) session.load(EquipmentSheet.class, equipmentSheetNO);
        List<TemporaryCarParts> TemporaryCarPartsList = session.createQuery("from TemporaryCarParts where equipmentSheetNo=:equipmentSheetNo").setParameter("equipmentSheetNo", equipmentSheetNO).list();


        for (TemporaryCarParts t:
                TemporaryCarPartsList) {
           CarParts c = (CarParts)session.createQuery("from CarParts where carPartsName=:carPartsName").setParameter("carPartsName", t.getCarPartsName()).uniqueResult();
           c.setCarPartsCount(c.getCarPartsCount()+t.getCarPartsCount());
            session.update(c);
            session.delete(t);
            session.flush();
        }
        
        session.delete(e);

        //根据用品的名字改变配件库配件的数量

        session.flush();
        session.close();

    }

    @Override
    public Integer getRow(Map<String, Object> map) {

        StringBuffer hql = new StringBuffer("select count(*) from  EquipmentSheet e,Car c,Customer cu where c.carId=e.car.carId and cu.customerId=e.customer.customerId");
       /* StringBuffer hql = new StringBuffer("select " +
                "equipmentSheet.equipmentSheetNo,customer.customerId,customer.customerName," +
                "customer.customerTel,car.vin,equipmentSheet.openingTime,equipmentSheet.totalPrice," +
                "equipmentSheet.givenMoney,equipmentSheet.receivables,equipmentSheet.received,equipmentSheet.equipmentSheetState"+
                " from  equipmentSheet,car,customer  where 1=1 and car.carId=equipmentSheet.carId and customer.customerId=equipmentSheet.customerId");
*/


            if (map.get("equipmentSheetNo") != null && !"".equals(map.get("equipmentSheetNo"))) {
                hql.append(" and e.equipmentSheetNo like '%" + map.get("equipmentSheetNo") + "%'");
            }
            if (map.get("customerName") != null && !"".equals(map.get("customerName"))) {
                hql.append(" and e.customer.customerName like '%" + map.get("customerName") + "%'");
            }
            if (map.get("vin") != null && !"".equals(map.get("vin"))) {
                hql.append(" and e.car.vin like '%" + map.get("vin") + "%'");
            }
            if (map.get("carCard") != null && !"".equals(map.get("carCard"))) {
                hql.append(" and e.car.carCard like '%" + map.get("carCard") + "%'");
            }

            if (map.get("upholsteryType") != null && !"-1".equals(map.get("upholsteryType"))) {
                hql.append(" and e.upholsteryType.upholsteryType ='" + map.get("upholsteryType")+"'");
            }
            if (map.get("openingTime1") != null && !"".equals(map.get("openingTime1"))) {
                hql.append(" and e.openingTime >='" + map.get("openingTime1")+"'");
            }
            if (map.get("openingTime2") != null && !"".equals(map.get("openingTime2"))) {
                hql.append(" and e.openingTime <='" + map.get("openingTime2")+"'");
            }
            if (map.get("equipmentSheetState") != null && !"-1".equals(map.get("equipmentSheetState"))) {
                hql.append(" and e.equipmentSheetState ='" + map.get("equipmentSheetState")+"'");
            }



        Session session = getSessionFactory().openSession();


        Query query = session.createQuery(hql.toString());

        Integer count=((Long) query.setCacheable(true).uniqueResult()).intValue();


        session.close();









       return count;
    }

    @Override
    public void setEquipmentSheetType(Integer EquipmentSheetNo) {

        EquipmentSheet equipmentSheet = getHibernateTemplate().get(EquipmentSheet.class, EquipmentSheetNo);
        equipmentSheet.setEquipmentSheetState("正在装潢");
        getHibernateTemplate().save(equipmentSheet);
    }


}
